package com.legal.utils;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class AddressUtils {
    private static final String API_URL = "https://apis.map.qq.com/ws/place/v1/search";
    private static final String appKey = "6LDBZ-BF4HK-G2ZJA-ABCNH-3A44S-7OFZI";

    public static String getAddress(String address) {
        RestTemplate restTemplate = new RestTemplate();
        String region="region("+address+",0)";
        try {
            String keyword = URLEncoder.encode("人民法院", "UTF-8");  // 更精确的关键词
            String boundary = URLEncoder.encode(region, "UTF-8");  // 更精确的行政区划

            String url = String.format("%s?key=%s&keyword=%s&boundary=%s",
                    API_URL,
                    appKey,
                    keyword,
                    boundary);

            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
            return response.getBody();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }
}
